{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "256a78a1",
   "metadata": {},
   "source": [
    "# 什么是统计学意义测试？\n",
    "在统计学中，统计学意义意味着产生的结果背后有一个原因，它不是随机产生的，也不是偶然的。\n",
    "\n",
    "SciPy为我们提供了一个叫scipy.stats的模块，它有执行统计学意义测试的函数。\n",
    "\n",
    "这里有一些技术和关键词，在进行这种测试时很重要。\n",
    "\n",
    "## 统计学中的假设\n",
    "假设是对人口中某个参数的假设。\n",
    "\n",
    "## 空白假说\n",
    "它假定观察结果没有稳定的意义。\n",
    "\n",
    "## 替代假说\n",
    "它假定观察结果是由于某种原因造成的。\n",
    "\n",
    "它是空头假设的替代品。\n",
    "\n",
    "例子。\n",
    "\n",
    "对于一个学生的评估，我们将采取。\n",
    "\n",
    "\"学生比平均水平差\"--作为无效假设，而。\n",
    "\n",
    "\"学生比平均水平好\"--作为另一个假设。\n",
    "\n",
    "## 单尾检验\n",
    "当我们的假设只测试一边的数值时，它被称为 \"单尾测试\"。\n",
    "\n",
    "例子。\n",
    "\n",
    "对于无效假设。\n",
    "\n",
    "\"平均数等于k\"，我们可以有另一个假设。\n",
    "\n",
    "\"平均数小于k\"，或者。\n",
    "\n",
    "\"平均数大于k\"\n",
    "\n",
    "## 双尾检验\n",
    "当我们的假设是对两边的数值进行检验。\n",
    "\n",
    "例子。\n",
    "\n",
    "对于无效假设。\n",
    "\n",
    "\"平均数等于k\"，我们可以有另一个假设。\n",
    "\n",
    "\"平均数不等于k\"\n",
    "\n",
    "在这种情况下，平均数小于，或大于k，两边都要检查。\n",
    "\n",
    "## 阿尔法值\n",
    "α值是显著性水平。\n",
    "\n",
    "例如。\n",
    "\n",
    "数据必须多接近于极端值才能拒绝无效假设。\n",
    "\n",
    "通常取0.01，0.05，或0.1。\n",
    "\n",
    "## P值\n",
    "P值告诉我们数据实际上有多接近极端。\n",
    "\n",
    "P值和α值进行比较以确定统计学意义。\n",
    "\n",
    "如果P值<=α，我们拒绝无效假设，并说数据具有统计学意义。\n",
    "\n",
    "# T-检验\n",
    "T检验用于确定两个变量的平均值之间是否存在显著的差异，并让我们知道它们是否属于同一分布。\n",
    "\n",
    "它是一个双尾检验。\n",
    "\n",
    "函数ttest_ind()取两个相同大小的样本，并产生一个t统计量和p值的元组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cf2dede6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ttest_indResult(statistic=0.39369923040284727, pvalue=0.6942267435155127)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from scipy.stats import ttest_ind\n",
    "\n",
    "v1 = np.random.normal(size=100)\n",
    "v2 = np.random.normal(size=100)\n",
    "\n",
    "res = ttest_ind(v1, v2)\n",
    "\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5594b1d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.6942267435155127\n"
     ]
    }
   ],
   "source": [
    "res = ttest_ind(v1, v2).pvalue\n",
    "\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "748b76cb",
   "metadata": {},
   "source": [
    "# KS-测试\n",
    "KS检验用于检查给定值是否遵循分布。\n",
    "\n",
    "该函数将待测值和CDF作为两个参数。\n",
    "\n",
    "CDF可以是一个字符串，也可以是一个返回概率的可调用函数。\n",
    "\n",
    "它可以作为一个单尾或双尾测试使用。\n",
    "\n",
    "默认情况下，它是双尾的。我们可以将参数替代作为一个字符串，即双侧、较小或较大中的一个。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "83bd5ac8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "KstestResult(statistic=0.07734036591535798, pvalue=0.5614366689765357)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from scipy.stats import kstest\n",
    "\n",
    "v = np.random.normal(size=100)\n",
    "\n",
    "res = kstest(v, 'norm')\n",
    "\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30ce5f79",
   "metadata": {},
   "source": [
    "# 数据的统计描述\n",
    "为了查看一个数组中的数值摘要，我们可以使用describe()函数。\n",
    "\n",
    "它返回以下描述。\n",
    "\n",
    "观测值的数量(nobs)\n",
    "\n",
    "最小值和最大值 = minmax\n",
    "\n",
    "平均值\n",
    "\n",
    "方差\n",
    "\n",
    "偏度\n",
    "\n",
    "峰度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "121ace84",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DescribeResult(nobs=100, minmax=(-1.7607831353162464, 2.690788027933544), mean=0.1954475751671897, variance=1.0347563768203658, skewness=0.1786320757197118, kurtosis=-0.8540687941296627)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from scipy.stats import describe\n",
    "\n",
    "v = np.random.normal(size=100)\n",
    "res = describe(v)\n",
    "\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43f01d0b",
   "metadata": {},
   "source": [
    "# 正态性测试（偏度和峰度）\n",
    "正态性检验是基于偏度和峰度的。\n",
    "\n",
    "normaltest()函数返回无效假设的p值。\n",
    "\n",
    "\"X来自于正态分布\"。\n",
    "\n",
    "## 偏度\n",
    "对数据对称性的衡量。\n",
    "\n",
    "对于正态分布，它是0。\n",
    "\n",
    "如果它是负的，意味着数据向左倾斜。\n",
    "\n",
    "如果它是正数，意味着数据向右倾斜。\n",
    "\n",
    "## 峰度\n",
    "衡量数据是重尾还是轻尾的正态分布。\n",
    "\n",
    "正的峰度意味着重尾。\n",
    "\n",
    "负的峰度意味着轻度尾巴。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7947bab6",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-0.014425289267863966\n",
      "-0.6699144837464184\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from scipy.stats import skew, kurtosis\n",
    "\n",
    "v = np.random.normal(size=100)\n",
    "\n",
    "print(skew(v))\n",
    "print(kurtosis(v))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ed528224",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NormaltestResult(statistic=0.4470430382679166, pvalue=0.7996976828808714)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from scipy.stats import normaltest\n",
    "\n",
    "v = np.random.normal(size=100)\n",
    "\n",
    "print(normaltest(v))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
